package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Order;
import com.itheima.service.OrderService;
import com.itheima.utils.SMSUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 预约管理
   */
@RequestMapping("/order")
@RestController
public class OrderController {
    @Reference
    private OrderService orderService;
    //预约分页查询
    @RequestMapping("/queryPage")
    public PageResult findOrders(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = orderService.findOrderByPage(queryPageBean);
        return pageResult;
    }
    //电话人工服务新增预约
    @RequestMapping("/addByTel")
    public Result submit(@RequestBody Map<String, String> map, Integer[] setmealIds) {

        map.put("orderType", Order.ORDERTYPE_TELEPHONE);//设置预约类型，分为微信预约、电话预约
        Result result = null;
        try {
            result = orderService.orderByTel(map, setmealIds);//通过Dubbo远程调用服务实现在线预约业务处理
        } catch (Exception e) {
            e.printStackTrace();
            return result;
        }
        return result;
    }
    /**
     * 根据 orderId 查询 手机号 和 预约日期
     * 并发送短信
     *
     * @param orderId
     * @return
     * @throws Exception
     */
    @RequestMapping("/confirmOrder/{orderId}")
    public Result confirmOrder(@PathVariable("orderId") Integer orderId) throws Exception {
        try {
            Map orderInfo = orderService.find2DetailsById(orderId);
            // 手机号 和 预约日期
            String telephone = (String) orderInfo.get("telephone");
            String orderDate = (String) orderInfo.get("orderDate");
            // 发送短信
            SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE, telephone, orderDate);
            return new Result(true, MessageConstant.CONFIRM_ORDER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.CONFITM_ORDER_FAIL);
        }
    }

    /**
     * 取消预约 根据 orderId 删除该预约信息 根据 orderDate 更新当日预约人数
     *
     * @param orderInfo
     * @return
     * @throws Exception
     */
    @RequestMapping("/cancelOrder")
    public Result cancelOrder(@RequestBody Map<String, String> orderInfo) throws Exception {
        // 获取参数
        String orderIdStr = orderInfo.get("orderId");
        Integer orderId = null;
        if (orderIdStr != null && orderIdStr.length() > 0) {
            orderId = Integer.parseInt(orderIdStr);
        }
        String orderDate = orderInfo.get("orderDate");
        try {
            orderService.cancelOrder(orderId, orderDate);
            return new Result(true, MessageConstant.CANCEL_ORDER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.CANCEL_ORDER_FAIL);
        }
    }
}
